﻿using DocumentFormat.OpenXml.Packaging;
using NPOI.HPSF;
using PdfSharpCore.Drawing;
using PdfSharpCore.Fonts;
using PdfSharpCore.Pdf;
using Quartz;
using System;
using System.Collections.Generic;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wittur.GenereateKongCoCPDF.Core.Common;

namespace Wittur.GenereateKongCoCPDF.Core
{
    [DisallowConcurrentExecution]
    public class TestGenPDF : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            await GenPDF();
            await Task.CompletedTask;
        }

        private async Task GenPDF()
        {
            try
            {
                PdfDocument pdf = new PdfDocument();
                PdfPage page = pdf.AddPage();
                XGraphics gfx = XGraphics.FromPdfPage(page);

                XFont cfont = new XFont("simsun", 12);
                XBrush brush = XBrushes.Black;

                // 中文
                gfx.DrawString("威特电梯部件（苏州）有限公司", cfont, brush, new XPoint(50, 110));

                XFont efont12 = new XFont("Arial", 12);
                gfx.DrawString("WITTUR  ELEVATOR COMPONENTS (SUZHOU) CO.LTD.", efont12, brush, new XPoint(50, 124));

                XFont efont22 = new XFont("Arial", 22);
                gfx.DrawString("Products Quality Certificate", efont22, brush, new XPoint(120, 194));

                XFont cDXfont22 = new XFont("等线", 22);
                gfx.DrawString("产品合格证", cDXfont22, brush, new XPoint(120, 210));


                // 插入图片
                var imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images", "wittur.png");
                XImage image = XImage.FromFile(imagePath);
                gfx.DrawImage(image, 473, 75, 60, 40); // 设置图片位置和大小

                var outPutPath = ConfigHelper.Instance.GetString("Path", "PdfGenDir");
                var filePath = Path.Combine(outPutPath, "2025-03-19", "PdfSharp生成测试8.pdf");
                pdf.Save(filePath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
        }
    }
    public class CustomFontResolver : IFontResolver
    {
        public string DefaultFontName => "";

        public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
        {
            if (familyName.Equals("simsun", StringComparison.OrdinalIgnoreCase))
            {
                return new FontResolverInfo("simsun#");
            }
            else if (familyName.Equals("Arial", StringComparison.OrdinalIgnoreCase))
            { 
                return new FontResolverInfo("Arial#");
            }
            else if (familyName.Equals("等线", StringComparison.OrdinalIgnoreCase))
            {
                return new FontResolverInfo("等线#");
            }
            return null;
        }

        public byte[] GetFont(string faceName)
        {
            if (faceName.StartsWith("simsun"))
            {
                return File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ttf", "simsun.ttf"));
            }
            else if (faceName.StartsWith("Arial"))
            { 
                return File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ttf", "Arial.ttf"));
            }
            else if (faceName.StartsWith("等线"))
            {
                return File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ttf", "等线.ttf"));
            }
            return null;
        }
    }
}
